#!bin/env python

import os
import xml.etree.ElementTree as ET

config_key_hint = {
    "test_case_path" : "输入测试存放路径",
    "coverage_path" : "输入编译环境路径",
}
config_section = "last_config"
config_filename = "./xts_config.ini"

def modify_xts_settings(configs):
    settings_filename = "./workspace/developertest/config/user_config.xml"
    tree = ET.parse(settings_filename)
    root = tree.getroot()
    test_case = root.find("test_cases").find("dir")
    test_case.text = configs["test_case_path"]
    coverage = root.find("coverage").find("outpath")
    coverage.text = configs["coverage_path"]
    tree.write(settings_filename)

def load_default_config():
    default_config = {}
    default_config["test_case_path"] = "../.."
    default_config["coverage_path"] = "/home/openharmony/master/out/RK3558"
    return default_config

def interact_config(config):
    for key in config_key_hint.keys():
        config[key] = input(config_key_hint[key] + "(缺省值: " + config[key] +"):") or config[key]
    return config

def hint_pip_mirror():
    print(r'''请设置你的pip源, 以方便后续步骤能正常下载相关依赖
(为了不覆盖pip源设置, 此步骤不自动化处理, 需要手动介入...)

考虑将如下pip镜像源设置到的你的系统路径: C:\Users\<username>\pip\pip.ini
[global]
index-url = https://repo.huaweicloud.com/repository/pypi/simple
format = columns
trusted-host = repo.huaweicloud.com
timeout = 120
''')
    input("回车继续...")

def hint_helper():
    print(r'''
***********************欢迎使用OpenHarmony XTS Helper*********************************
这是一个XTS环境设置的脚本, 避免在手动修改相关配置造成的错误中迷失, 试图提供一些自动化的帮助。
(项目起源于作者跑XTS无法生成用来报告, 经过反复检查, 最后发现时配置理解出错导致,
所以有了这个半自动化的配置脚本工程, 试图降低使用门槛)
如果它帮到你, 请star这个项目~

author: waibozie waibozie@outlook.com
repo: https://gitee.com/waibozie/openhamony_xts_helper

欢迎提PR加入你想要的功能或者问题修复
*************************************************************************************
''')

def setup_xts():
    os.system("cd workspace/xdevice && python setup.py install")

def main():
    hint_helper()

    print("STEP 1/3: 配置pip源")
    hint_pip_mirror()

    print("STEP 2/3: 配置路径（XTS用例路径 与 编译环境路径）")
    config = load_default_config()
    config = interact_config(config)
    modify_xts_settings(config)

    print("STEP 3/3: 启动XTS setup")
    setup_xts()

    print("Done! Enjoy it.")

if __name__ == "__main__":
    main()
